package com.xxxx.yebserver.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xxxx.yebserver.common.R;
import com.xxxx.yebserver.entity.Menu;
import com.xxxx.yebserver.entity.MenuRole;
import com.xxxx.yebserver.service.MenuRoleService;
import com.xxxx.yebserver.service.MenuService;
import com.xxxx.yebserver.vo.MenusWithRoleVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cxq
 * @since 2021-06-15
 */
@RestController
@RequestMapping("/system/cfg")
public class MenuController {
    @Autowired
    MenuService menuService;

    @Autowired
    MenuRoleService menuRoleService;

    @ApiOperation("获取登录用户的所有菜单权限")
    @GetMapping("menus")
    public R menus(){
        List<Menu> menus = menuService.getMenusByAdminId();
        return R.ok().data(menus);
    }


    @ApiOperation("获取所有用户组所对应的权限")
    @GetMapping("menusWithRole")
    public R menusWithRole(){
        List<MenusWithRoleVo> menus = menuService.getMenusWithRole();
        return R.ok().data(menus);
    }

    @ApiOperation("获取所有菜单")
    @GetMapping("treeMenus")
    public R getAllTreeMenus(){
        List<Menu> menus = menuService.getAllTreeMenus();
        return R.ok().data(menus);
    }


    @ApiOperation("根据角色ID查询菜单ID")
    @GetMapping("/mid/{rid}")
    public List<Long> getMidByRid(@PathVariable("rid") Long rid){
        List<MenuRole> menuRoles = menuRoleService.list(new QueryWrapper<MenuRole>().eq("rid", rid));
        return menuRoles.stream().map(item->item.getMenuId()).collect(Collectors.toList());
    }

    @ApiOperation("更新角色菜单")
    @PutMapping("/")
    public R updateMenuRoles(Long rid, Long[] mids){
        Boolean save = menuService.updateMenuRoles(rid,mids);
        if(save) return R.ok();
        return R.error();
    }
}

